var alertBoxApp = window.parent.alertBoxApp;
//get pkg types
var pkgTypes = [];
appAjax("pkgType.json",
		null,
		null,
		null,
		function(data){
            pkgTypes = data;
},null);



/**
 * artifact app
 */
var artifactTableApp = new Vue({
	el:"#artifactTable",
	data:{
		pkgs:[],
	},
	methods:{	

		resetCheck:function(){
			$("#artifactTable > tbody > tr > td > .checkbox-inline > span").each(function(index,element){
				$(element).html("");
			});
		},
		getCheckedIds:function(){
			var checkedIds = [];
			$("#artifactTable > tbody > tr > td > .checkbox-inline").each(function(index,element){
				if($(element).find("span > i").length>0){
					checkedIds.push($(element).find("input[type=hidden]").val());
				}
			});
			return checkedIds;
		},
		getCheckedIndexs:function(){
			var checkedIndexs = [];
			$("#artifactTable > tbody > tr > td > .checkbox-inline").each(function(index,element){
				if($(element).find("span > i").length>0){
					checkedIndexs.push(index);
				}
			});
			return checkedIndexs;
		},
		getCheckedElms:function(){
			var checkedElms = [];
			var checkedIds = this.getCheckedIds();
			for (var i = 0; i < checkedIds.length; i++) {
				for (var j = 0; j < this.pkgs.length; j++) {
					if(this.pkgs[j].pkgFullName == checkedIds[i]){
						checkedElms.push(this.pkgs[j]);
						break;
					}
				}
			}
			return checkedElms;
		},
        getArtsBySearch:function(searchText){
			var that = this;
			appAjax("pkg",
				{keyWord:searchText},
				null,
				null,
				function(data){
					that.pkgs = data;
				},
				null
				);
		}
		
	}
	
	
});

/**
 * add artifact
 */
var createModalApp = new Vue({
	el:"#createModal",
	data:{
		pkg:{
            groupId:"",
            artifactId:"",
            versionId:""
		},
        myUploadFile:{}

	},
	mounted:function(){
		var that =this;
		$("#createModal").on('hide.bs.modal',function(){
			that.pkg={
                groupId:"",
                artifactId:"",
                versionId:""
			};
			that.myUploadFile={};
			//artifact table reshow
            artifactTableApp.getArtsBySearch(searchFormApp.searchText);
		});
	},
	computed:{
		
	},
	methods:{

        choosePkgType:function(pkgType){
			this.pkg.packaging = pkgType;
			this.choosedPkgType = pkgType;
		},
        onFileChange:function(e) {
            var files = e.target.files || e.dataTransfer.files;
            if (!files.length)
                return;
            this.myUploadFile = e.target.files[0];
        },
		checkData:function(){
            var validatorGroup = [{
                valStr:this.pkg.groupId,
                valFunc:validator.isLength,
                valOption:{min:1},
                errorMes:"组织机构不能为空"
            },{
                valStr:this.pkg.artifactId,
                valFunc:validator.isLength,
                valOption:{min:1},
                errorMes:"构件名不能为空"
            },{
                valStr:this.pkg.versionId,
                valFunc:validator.isLength,
                valOption:{min:1},
                errorMes:"版本号不能为空"
            },{
                valStr:this.myUploadFile.name != undefined?this.myUploadFile.name:"",
                valFunc:validator.isLength,
                valOption:{min:1},
                errorMes:"文件不能为空"
			}];
            for(var i=0;i<validatorGroup.length;i++){
                var validatorItem = validatorGroup[i];
                if(!validatorItem.valFunc(validatorItem.valStr,validatorItem.valOption)){
                    //alert error mes
                    alertBoxApp.add({
                        type:"error",
                        title:"构件增加",
                        content:validatorItem.errorMes
                    });
                    return false;
                }
            }
            return true;
		},
		createSave:function(){
			var that = this;
			if(this.checkData()){
				fileUploadAjax("pkg",
					that.pkg,
					'file',
					false,
					function(data){
                        var alertInfo = {title:"构件增加"};
                        if(data.obj){
                            alertInfo.content='文件上传成功！';
                            alertInfo.type="success";
                        }else{
                            alertInfo.content='文件上传类型不正确 ！';
                            alertInfo.type="error";
                        }
                        that.hide();

					});
            }

							
		},
		show:function(){
            $("#createModal").modal('show');
        },
        hide:function(){
            $("#createModal").modal('hide');
        }
		
	}
});



/**
 * artifact remove
 */
var sysConfirmApp = new Vue({
	el:"#sysConfirm",
	data:{
		message:"删除以下构件：",
		pkg:{}
	},
	mounted:function(){
		var that =this;
		$("#sysConfirm").on('hide.bs.modal',function(){			
			that.message="删除以下构件：";
            that.pkg={};
			//artifact table reshow
            artifactTableApp.getArtsBySearch(searchFormApp.searchText);
			//reset checkbox
            artifactTableApp.resetCheck();
		});
	},
	methods:{
		del:function(){
			var that =this;
			appAjax("pkg/"+that.pkg.pkgFullName,
					null,
					null,
					"DELETE",
					function(data){
						var alertInfo = {
								title:"构件删除"
						};
						if(data){
							alertInfo.type="success";
							alertInfo.content="删除构件"+that.pkg.pkgFullName+"成功";
						}else{
							alertInfo.type="error";
							alertInfo.content="删除构件"+that.pkg.pkgFullName+"失败";
						}
						alertBoxApp.add(alertInfo);
						//hide this
						that.hide();
			},function(){
				that.hide();
			});
	
		},
		show:function(){
			$("#sysConfirm").modal('show');	
		},
		hide:function(){
			$("#sysConfirm").modal('hide');
		}
	}
});
/**
 * 按钮组
 */
var artBtnGroupApp = new Vue({
	el:"#artBtnGroup",
	data:{
	},
	methods:{
		//检查选择的数量
		checkNum:function(option){
			var alertInfo={
				title:'构件操作',
				type:'warning'
			};
			var min = option.min;
			var max = option.max;
			var checkElms = artifactTableApp.getCheckedElms();
			if(checkElms.length<min){
				alertInfo.content="至少需要选择"+min+"个构件";
				alertBoxApp.add(alertInfo);
				return;
			}			
			if(max && checkElms.length>max){
				alertInfo.content="最多能够选择"+max+"个构件";
                alertBoxApp.add(alertInfo);
				return;
			}
			return checkElms;
			
		},
		add:function(){
				createModalApp.show();
		},
		del:function(){
			var checkElms =this.checkNum({min:1,max:1});
			if(checkElms){
				sysConfirmApp.pkg=checkElms[0];
				sysConfirmApp.show();
			}			
		}
	}
});

/**
 * serach form app
 */
var searchFormApp = new Vue({
	el:"#searchForm",
	data:{
		searchText:""
	},
	created:function(){
		this.search();
	},
	methods:{
		search:function(){
			artifactTableApp.getArtsBySearch(this.searchText);
		}
	}
	
});









